<?php
namespace Modules\Activity\Http\Controllers\Admin\RotateLottery;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\RotateLottery\Activity;
// use \Modules\Activity\Entities\ActivityRecord;

use Validator;
use ThrowException;
use DB;

class ActivityController extends Controller
{
    public function index(Request $req)
    {
        $rule = [
            // 'type' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $options = $req->all();

        $options['order_by'] = ['list_weight' => 'desc'];
        $options['with_count'] = ['skus'];
        // $options['with'] = ['activity_info'];

        $list = Activity::getList($options);

        $list->each(function ($item) {
            $item->sku_stock = $item->skus->sum('stock');
        });

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        // 
        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $info = $item->getInfo();

        return [
            'info' => $info
        ];
    }

    public function store(Request $req)
    {
        $rule = [
            'title' =>  ['required'],
            'thumb' => ['required'],
            'stock' => ['required']
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $info = $req->all();

        DB::beginTransaction();

        $item = Activity::create($info);

        DB::commit();

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    public function update(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        DB::beginTransaction();

        if ($req->type === 'update') {
            $info = $req->input('attributes');

            $item->update($info);
        }
        elseif ($req->type === 'copy') {
            $item->copy();
        }

        DB::commit();

        return [];
    }

    public function destroy(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

        $item = Activity::getEntity([
            'uuid' => $uuid
        ]);

        $item->delete();

        return [];
    }

    // // 用户列表
    // public function userIndex(Request $req, $uuid)
    // {
    //     $rule = [
    //         'type' => ['required']
    //     ];
    //     Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest(); 

    //     $options = $req->all();

    //     $activity = Activity::getEntity([
    //         'uuid' => $uuid
    //     ]);

    //     // if ($req->type === 'lucky') {
    //     //     $builder =  $activity->lucky_records(); 
    //     // }
    //     // elseif ($req->type === 'all') {
    //     //     $builder =  $activity->records(); 
    //     // }

    //     $list = ActivityRecord::getList([
    //         'builder' => $builder,
    //         'with' => ['user'],
    //         // 'with_count' => ['lottery_tickets', 'invitees'],
    //         'only' => [
    //             'id',
    //             'lottery_tickets_count',
    //             'invitees_count',
    //             'is_lucky_user',
    //             'created_at',
    //             'user' => [
    //                 'name',
    //                 'headimg',
    //                 'gender'
    //             ]
    //         ]
    //     ]);

    //     return $list->generateListResponse();
    // }
}
